package com.google.mr.okhttptest.fragment;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.mr.okhttptest.R;
import com.google.mr.okhttptest.bean.Mp3Info;
import com.google.mr.okhttptest.utils.MediaUtil;
import com.google.mr.okhttptest.view.RoundImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/11/27.
 */

public class MainFragment extends BaseFragment {

    @BindView(R.id.fragment_main_rv)
    RecyclerView fragmentMainRv;
    private List<Mp3Info> mp3InfoList;

    private static final int MY_PERMISSIONS_READ_EXTERNAL_STORAGE = 1;

    @Override
    public int getContentViewLayoutID() {
        return R.layout.fragment_main;
    }

    @Override
    protected void initView() {
        mp3InfoList = MediaUtil.getMp3Infos(getContext());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        fragmentMainRv.setLayoutManager(linearLayoutManager);
        MainFragmentAdapter mainFragmentAdapter = new MainFragmentAdapter();
        fragmentMainRv.setAdapter(mainFragmentAdapter);
        initPermission();
    }


    private void initPermission() {
        if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            // 没有权限。
            if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)) {
                // 用户拒绝过这个权限了，应该提示用户，为什么需要这个权限。
                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_READ_EXTERNAL_STORAGE);
            } else {
                // 申请授权。
                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_READ_EXTERNAL_STORAGE);
            }
        }
    }

    @Override
    protected void initData() {

    }

    private class MainFragmentAdapter extends RecyclerView.Adapter<MainFragmentAdapter.MediaplayHodler> {
        @Override
        public MediaplayHodler onCreateViewHolder(ViewGroup parent, int viewType) {
            MediaplayHodler mediaplayHodler = new MediaplayHodler(LayoutInflater.from(getContext()).inflate(R.layout.item_mediaplayer, null));
            return mediaplayHodler;
        }

        @Override
        public void onBindViewHolder(MediaplayHodler holder, int position) {
            MediaplayHodler mediaplayHodler = holder;
            mediaplayHodler.img_album.setImageBitmap(MediaUtil.getArtwork(mContext,mp3InfoList.get(position).getId(),mp3InfoList.get(position).getAlbumId(),true,true));
            mediaplayHodler.tv_title.setText(mp3InfoList.get(position).getTitle());
            mediaplayHodler.tv_artist.setText(mp3InfoList.get(position).getArtist());
        }

        @Override
        public int getItemCount() {
            return mp3InfoList==null?0:mp3InfoList.size();
        }

        class MediaplayHodler extends RecyclerView.ViewHolder{

            public RoundImageView img_album;
            public TextView tv_title;
            public TextView tv_artist;
            public MediaplayHodler(View itemView) {
                super(itemView);
                img_album = itemView.findViewById(R.id.img_album);
                tv_title = itemView.findViewById(R.id.tv_title);
                tv_artist = itemView.findViewById(R.id.tv_artist);
            }
        }
    }
}
